﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseLinQ.DataContex;
using DevExpress.Web.ASPxGridView;
using Module;
using Core;
using System.Data;
using RBAC;
using Solution.AppCode;
using OfficeOpenXml;
namespace Solutions.Admin.QuanLyNguoiDung
{

    public partial class QuanLyNguoiDung : System.Web.UI.UserControl
    {
        string strTempFolder = ConfigurationSettings.AppSettings["TemplateFolder"];
        string strExportFolder = ConfigurationSettings.AppSettings["ExportFolder"];
        public string BAOCAOND_TEMP_FILE_NAME = "BaoCaoNguoiDung_Temp.xlsx";
        public string BAOCAOND_NEW_FILE_NAME = "BaoCaoNguoiDung.xlsx";
        string _conn = ConfigurationSettings.AppSettings["ConnectString"];
        RBACDataContext db = null;
        long idVaiTro = 0;
        long idUser = 0;
        long DonViThaoTac = 0;
        Utils util = new Utils();
        public static long _idNguoiDung;
        protected void XuatExcelClick(object sender, EventArgs e)
        {
            FileInfo fileTemp = null;
            FileInfo fileNew = null;

            try
            {
                NguoiDungLogic obj = new NguoiDungLogic();
                DonViLogic objdv = new DonViLogic();
                DataTable lstsp = obj.GetListPaging("pk_iNguoIDungID","DESC","", UtilsData.GetUserId().ToString());
                if (lstsp != null && lstsp.Rows.Count > 0)
                {
                    String strPathNew = ResolveUrl("~/") + strExportFolder + "/" + BAOCAOND_NEW_FILE_NAME;
                    String strPathTemp = ResolveUrl("~/") + strTempFolder + "/" + BAOCAOND_TEMP_FILE_NAME;
                    fileNew = new FileInfo(Server.MapPath(strPathNew));
                    if (fileNew.Exists)
                    {
                        try
                        {
                            fileNew.Delete();
                        }
                        catch { }
                    }
                    fileTemp = new FileInfo(Server.MapPath(strPathTemp));
                    ExcelPackage xlPackage = new ExcelPackage(fileNew, fileTemp);
                    ExcelWorksheet worksheet = null;
                    if (xlPackage.Workbook.Worksheets.Count > 0)
                    {
                        worksheet = xlPackage.Workbook.Worksheets[1];
                    }
                    int iRow = 6;
                    int iCol = 1;
                    db = new RBACDataContext(_conn);
                    worksheet.Cells[1, 2].Value = (from m in db.tbl_Donvis
                                                  where m.pk_iDonViID == UtilsData.GetUserDepartment()
                                                  select m.sTenDonVi).FirstOrDefault();

                    worksheet.Cells[3, 2].Value = DateTime.Now.ToShortDateString();
                    for (int i = 0; i < lstsp.Rows.Count; ++i)
                    {
                        worksheet.Cells[iRow, iCol].Value = i + 1;
                        worksheet.Cells[iRow, iCol + 1].Value = lstsp.Rows[i]["sHoTen"];
                        worksheet.Cells[iRow, iCol + 2].Value = lstsp.Rows[i]["sTenDangNhap"];
                        worksheet.Cells[iRow, iCol + 3].Value = lstsp.Rows[i]["sTenDonVi"];
                        worksheet.Cells[iRow, iCol + 4].Value = lstsp.Rows[i]["sSoDienThoai"];
                        worksheet.Cells[iRow, iCol + 5].Value = lstsp.Rows[i]["sEmail"];
                        worksheet.Cells[iRow, iCol + 6].Value = lstsp.Rows[i]["sGhiChu"];
                        iRow++;
                        for (int j = 0; j < 7; j++)
                        {
                            var border = worksheet.Cells[iRow - 1, iCol + j].Style.Border;
                            border.Bottom.Style = border.Top.Style = border.Right.Style = border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                            worksheet.Cells[iRow - 1, iCol + j].Style.Border = border;
                        }
                    }
                    worksheet.View.PageLayoutView = false;
                    worksheet.Cells.AutoFitColumns();
                    xlPackage.Save();
                    xlPackage.Dispose();
                    //Download
                    Response.Redirect(ResolveUrl("~/Download/Download.aspx?type=BaoCaoNguoiDung"));
                    lstsp = null;
                }

            }
            catch (Exception ex)
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (grdQuanLyNguoiDung.IsCallback || !Page.IsPostBack)
            {
                
                LoadNguoiDung();
                tbl_NguoiDung nd = UtilsData.GetUserInfo();
                if (nd.fk_iDonViID == 1)
                {
                    cboDonVi.Visible = true;
                }
                else
                {
                    cboDonVi.Visible = false;
                }

                btnThemMoi.Visible = true;
                grdQuanLyNguoiDung.Columns[6].Visible = true;
                grdQuanLyNguoiDung.Columns[7].Visible = true;
                grdQuanLyNguoiDung.Columns[8].Visible = true;

                btnAddNewPhanQuyen.Visible = true;
                grdDanhSachQuyen.Columns[4].Visible = true;
                grdDanhSachQuyen.Columns[5].Visible = true;

                //Thao tác tài nguyên quản lý người dùng
                DonViThaoTac = GetDonViThaoTac();
                if (!util.checkQuyenThaoTac(idUser, DonViThaoTac, idVaiTro, "QuanLyNguoiDung", "NGUOI_DUNG", Constants.ACTION_CREATE))
                {
                    btnThemMoi.Visible = false;
                }
                if (!util.checkQuyenThaoTac(idUser, DonViThaoTac, idVaiTro, "QuanLyNguoiDung", "NGUOI_DUNG", Constants.ACTION_UPDATE))
                {
                    grdQuanLyNguoiDung.Columns[6].Visible = false;
                }
                if (!util.checkQuyenThaoTac(idUser, DonViThaoTac, idVaiTro, "QuanLyNguoiDung", "NGUOI_DUNG", Constants.ACTION_DELETE))
                {
                    grdQuanLyNguoiDung.Columns[7].Visible = false;
                }

                //Thao tác tài nguyên phân quyền người dùng
                if (!util.checkQuyenThaoTac(idUser, DonViThaoTac, idVaiTro, "QuanLyNguoiDung", "VT_NGUOI_DUNG", Constants.ACTION_READ))
                {
                    grdQuanLyNguoiDung.Columns[8].Visible = true;
                }

                if (!util.checkQuyenThaoTac(idUser, DonViThaoTac, idVaiTro, "QuanLyNguoiDung", "VT_NGUOI_DUNG", Constants.ACTION_CREATE))
                {
                    btnAddNewPhanQuyen.Visible = false;
                }

                if (!util.checkQuyenThaoTac(idUser, DonViThaoTac, idVaiTro, "QuanLyNguoiDung", "VT_NGUOI_DUNG", Constants.ACTION_UPDATE))
                {
                    grdDanhSachQuyen.Columns[4].Visible = false;
                }

                if (!util.checkQuyenThaoTac(idUser, DonViThaoTac, idVaiTro, "QuanLyNguoiDung", "VT_NGUOI_DUNG", Constants.ACTION_DELETE))
                {
                    grdDanhSachQuyen.Columns[5].Visible = false;
                }
                LoadCboDonVi();
                LoadCboVaiTro(idUser, DonViThaoTac);
                //mcboVaiTroForm.Show();
                //mcboVaiTroForm.AllowBlank = true;
                //cboVaiTroForm.Hide();
                //cboVaiTroForm.AllowBlank = false;
            }
        }
        
        protected void LoadNguoiDung()
        {
            NguoiDungLogic logic = new NguoiDungLogic();
            string strSort = "sHoTen";
            string strDir = "ASC";
            string strFilter = "";
            string NguoiDungID = Utils.GetUserId().ToString();
            var lstMenu = logic.GetListPaging(strSort, strDir, strFilter, NguoiDungID);
            if (lstMenu != null)
            {
                grdQuanLyNguoiDung.DataSource = lstMenu;
                grdQuanLyNguoiDung.DataBind();
                lstMenu = null;
            }
        }
        public long GetDonViThaoTac()
        {
            idVaiTro = long.Parse(Utils.GetSession_value(Response, Page, Constants.session_vaitroquanly_id));
            idUser = long.Parse(Utils.GetSession_value(Response, Page, Constants.session_user_id));
            return util.LayDonviThaoTac(idVaiTro, idUser, "NGUOI_DUNG", Constants.ACTION_READ, "QuanLyNguoiDung");
        }

        public void LoadCboDonVi()
        {
            DonViLogic logicDV = new DonViLogic();
            List<tbl_Donvi> lstDonVi = logicDV.GetTreeDonVi(GetDonViThaoTac());
            cboDonViQuanLy.DataSource = lstDonVi;
            cboDonViQuanLy.DataBind();
            cboDonVi.DataSource = lstDonVi;
            cboDonVi.DataBind();
            lstDonVi = null;
        }

        public void LoadCboVaiTro(long _lngUserID, long _lngDonViID)
        {
            VaiTroLogic logic = new VaiTroLogic();
            List<tbl_VaiTro> lstVaiTro = logic.GetListByNguoiDungID(_lngUserID, _lngDonViID);
            if (lstVaiTro != null && lstVaiTro.Count > 0)
            {
                cboQuyen.DataSource = lstVaiTro;
                cboQuyen.DataBind();
            }
            
            logic = null;
        }
        public void LoadWindowRoleByUserID(long _lngUserID)
        {
            NguoiDungLogic nguoiDungLogic = new NguoiDungLogic();
            PhanQuyenNguoiDungLogic phanQuyenLogic = new PhanQuyenNguoiDungLogic();
            tbl_NguoiDung objNguoiDung = nguoiDungLogic.GetByID(_lngUserID);
            long lngNguoiDungDangNhapID = Utils.GetUserId();
            if (objNguoiDung != null)
            {
                //Hiển thị thông tin người dùng
                lblHoTenNguoiDung.Text ="<b>" + objNguoiDung.sTenDangNhap + "</b>";
                lblTenDangNhapNguoiDung.Text ="<b>" + objNguoiDung.sTenDangNhap + "</b>";
                //Hiển thị danh sách quyền
                DataTable dtblPhanQuyen = phanQuyenLogic.GetListByNguoiDungID(_lngUserID);
                if (dtblPhanQuyen != null && dtblPhanQuyen.Rows.Count > 0)
                {
                    grdDanhSachQuyen.DataSource = dtblPhanQuyen;
                    grdDanhSachQuyen.DataBind();
                }
            }
            phanQuyenLogic = null;
            nguoiDungLogic = null;
        }
        public void grdQuanLyNguoiDung_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e)
        {
            NguoiDungLogic logic = new NguoiDungLogic();
            int id = Convert.ToInt32(e.KeyValue);
            hdIdNguoiDung.Text = id + "";
            switch (e.CommandArgs.CommandName)
            {
                case "Edit":
                    try
                    {
                        LoadFromByID(id);
                        btnLuuLai.Visible = false;
                        btnLuuSua.Visible = true;
                        txtTenDangNhap.ReadOnly = true;
                        wdsQuanLyNguoiDung.ShowOnPageLoad = true;
                    }
                    catch (Exception ex)
                    {

                    }
                    break;
                case "Delete":
                    try
                    {
                        if (logic.Delete(id))
                        {
                            Tools.Message(this.Page, "Xóa thành công!");
                            LoadNguoiDung();
                        }
                        else
                        {
                            Tools.Message(this.Page, "Xóa thất bại!");
                        }
                    }
                    catch (Exception ex)
                    {
                        Tools.Message(this.Page, "Xóa thất bại");
                    }
                    break;
                case "Roles":
                    try
                    {
                        wdsPhanQuyen.ShowOnPageLoad = true;
                        btnLuuGanQuyen.Visible = false;
                        btnLuuLaiQuyen.Visible = false;
                        btnDongQuyen.Visible = true;
                        cboDonViQuanLy.ReadOnly = true;
                        cboQuyen.ReadOnly = true;
                        LoadWindowRoleByUserID(id);
                    }
                    catch (Exception ex)
                    {

                    }
                    break;
            }
        }
        public void LoadFromPhanQuyenByID(long _lngVaiTroNguoiDungID)
        {
            PhanQuyenNguoiDungLogic logic = new PhanQuyenNguoiDungLogic();
            tbl_VaiTroNguoiDungDonVi objVaiTroNguoiDung = logic.GetByID(_lngVaiTroNguoiDungID);
            if (objVaiTroNguoiDung != null && objVaiTroNguoiDung.pk_iVaiTroNguoiDungID > 0)
            {
                cboQuyen.Text = objVaiTroNguoiDung.fk_iVaiTroID + "";
                cboDonViQuanLy.Text = objVaiTroNguoiDung.fk_iDonViID + "";
                txtsGhiChu.Text = objVaiTroNguoiDung.sGhiChu;
            }
            logic = null;
        }
        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            wdsQuanLyNguoiDung.ShowOnPageLoad = true;
            ResetForm();
            btnLuuSua.Visible = false;
            btnLuuLai.Visible = true;
            cboDonVi.Focus();
            cboDonVi.Visible = true;
        }
        public long donvithaotac()
        {
            idVaiTro = long.Parse(Utils.GetSession_value(Response, Page, Constants.session_vaitroquanly_id));
            idUser = long.Parse(Utils.GetSession_value(Response, Page, Constants.session_user_id));
            return util.LayDonviThaoTac(idVaiTro, idUser, "NGUOI_DUNG", Constants.ACTION_READ, "QuanLyNguoiDung");
        }
        protected void btnLuuSua_Click(object sender, EventArgs e)
        {
            txtTenDangNhap.Text = Tools.ChuanHoaXau(txtTenDangNhap.Text);
            NguoiDungLogic logic = new NguoiDungLogic();


            if (cboDonVi.SelectedIndex == -1)
            {
                Tools.Message(this.Page,"Đơn vị không được để trống!");
                cboDonVi.Focus();
                return;
            }
            if (Tools.ChuanHoaXau(txtTenDangNhap.Text) == "")
            {
                Tools.Message(this.Page,"Tên đăng nhập không được để trống!");
                txtTenDangNhap.Focus();
                return;
            }
            if (Tools.ChuanHoaXau(txtEmail.Text) == "")
            {
                Tools.Message(this.Page,"Email không được để trống!");
                txtEmail.Focus();
                return;
            }
            tbl_NguoiDung user = logic.GetByID(long.Parse(Utils.GetSession_value(Response, Page, Constants.session_user_id)));
            tbl_NguoiDung objNguoiDung = GetObjectFromForm();
            long lngNguoiDungID = long.Parse("0" + hdIdNguoiDung.Text);
            tbl_NguoiDung objNguoiDungOld = logic.GetByID(lngNguoiDungID);
            if (logic.checkUser(txtTenDangNhap.Text.ToLower(), lngNguoiDungID) != null)
            {
                Tools.Message(this.Page,"Đã tồn tại tên đăng nhập này!");
                txtTenDangNhap.Focus();
                return;
            }
            objNguoiDung.sTenDangNhap = txtTenDangNhap.Text.ToLower();
            objNguoiDung.sHoTen = txtHoTen.Text;
            objNguoiDung.sEmail = txtEmail.Text;
            objNguoiDung.sSoDienThoai = txtDienThoai.Text;
            objNguoiDung.sGhiChu = txtGhiChu.Text;
            objNguoiDung.pk_iNguoiDungID = lngNguoiDungID;

            try
            {
                if (logic.Update(objNguoiDung))
                {
                    Tools.Message(this.Page,"Cập nhật thành công!");
                    LoadNguoiDung();
                }
                else
                {
                    Tools.Message(this.Page, "Cập nhật thất bại!");
                }
            }
            catch (Exception ex)
            {
                
            }
            wdsQuanLyNguoiDung.ShowOnPageLoad = false;
            ResetForm();
        }
        /// <summary>
        /// Lấy đối tượng từ form
        /// </summary>
        /// <returns></returns>
        public tbl_NguoiDung GetObjectFromForm()
        {
            tbl_NguoiDung objRes = new tbl_NguoiDung();
            objRes.sTenDangNhap = txtTenDangNhap.Text.Trim();
            objRes.sHoTen = txtHoTen.Text.Trim();
            objRes.sEmail = txtEmail.Text.Trim();
            objRes.sSoDienThoai = txtDienThoai.Text.Trim();
            objRes.sGhiChu = txtGhiChu.Text.Trim();

            objRes.fk_iNguoiCapNhatID = Utils.GetUserId();
            if (cboDonVi.Text == "" | cboDonVi.Value == null)
            {
                objRes.fk_iDonViID = Utils.GetRootDepartmentID(donvithaotac());
            }
            else
                objRes.fk_iDonViID = long.Parse(cboDonVi.Value.ToString());
            return objRes;
        }
        protected void ResetForm()
        {
            txtTenDangNhap.Text = "";
            txtHoTen.Text = "";
            txtEmail.Text = "";
            txtDienThoai.Text = "";
            txtGhiChu.Text = "";
            cboDonVi.Value = null;
            cboDonVi.Text = "";
        }
        protected void btnLuuLai_Click(object sender, EventArgs e)
        {
            NguoiDungLogic logic = new NguoiDungLogic();
            tbl_NguoiDung user = logic.GetByID(long.Parse(Utils.GetSession_value(Response, Page, Constants.session_user_id)));
            if (cboDonVi.SelectedIndex == -1)
            {
                Tools.Message(this.Page, "Đơn vị không được để trống!");
                cboDonVi.Focus();
                return;
            }
            if (Tools.ChuanHoaXau(txtTenDangNhap.Text) == "")
            {
                Tools.Message(this.Page, "Tên đăng nhập không được để trống!");
                txtTenDangNhap.Focus();
                return;
            }
            if (Tools.ChuanHoaXau(txtEmail.Text) == "")
            {
                Tools.Message(this.Page, "Email không được để trống!");
                txtEmail.Focus();
                return;
            }
            if (logic.GetByCode(txtTenDangNhap.Text.Trim()) != null)
            {
                Tools.Message(this.Page, "Tên đăng nhập đã tồn tại!");
                txtTenDangNhap.Focus();
                return;
            }
            tbl_NguoiDung objNguoiDung = GetObjectFromForm();
            tbl_NguoiDung Nd = logic.GetByID(Utils.GetUserId());
            objNguoiDung.bKichHoat = true;
            objNguoiDung.sMatKhau = Tools.HashPassword("123456Aa"); //Mặc định mật khẩu ban đầu của người dùng là 123456Aa
            objNguoiDung.fk_iNguoiTaoID = Nd.pk_iNguoiDungID;
            objNguoiDung.fk_iNguoiCapNhatID = Utils.GetUserId();
            objNguoiDung.sTenDangNhap = txtTenDangNhap.Text.ToLower();

            if (logic.Insert(objNguoiDung))
            {
                objNguoiDung.sPath = Nd.sPath + objNguoiDung.pk_iNguoiDungID + "/";
                logic.Update(objNguoiDung);
                Tools.Message(this.Page, "Thêm mới thành công!");
                LoadNguoiDung();
                ResetForm();
            }
            wdsQuanLyNguoiDung.ShowOnPageLoad = false;
        }
        public void LoadFromByID(long _lngNguoiDungID)
        {
            NguoiDungLogic logic = new NguoiDungLogic();
            tbl_NguoiDung objNguoiDung = logic.GetByID(_lngNguoiDungID);
            if (objNguoiDung != null && objNguoiDung.pk_iNguoiDungID > 0)
            {
                txtTenDangNhap.Text = objNguoiDung.sTenDangNhap;
                txtHoTen.Text = objNguoiDung.sHoTen;
                txtEmail.Text = objNguoiDung.sEmail;
                txtDienThoai.Text = objNguoiDung.sSoDienThoai;
                txtGhiChu.Text = objNguoiDung.sGhiChu;
                cboDonVi.Text = objNguoiDung.fk_iDonViID + "";
            }
            logic = null;
        }
        public void grdDanhSachQuyen_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e)
        {
            PhanQuyenNguoiDungLogic logic = new PhanQuyenNguoiDungLogic();
            int id = Convert.ToInt32(e.KeyValue);
            hdId.Text = id + "";
            switch (e.CommandArgs.CommandName)
            {
                case "Edit":
                    try
                    {
                        LoadFromPhanQuyenByID(id);
                        btnLuuGanQuyen.Visible = false;
                        btnLuuLaiQuyen.Visible = true;
                        btnDongQuyen.Visible = true;
                        cboDonViQuanLy.ReadOnly = false;
                        cboQuyen.ReadOnly = false;
                    }
                    catch (Exception ex)
                    {
                        
                    }
                    break;
                case "Delete":
                    try
                    {
                        if (logic.Delete(id))
                        {
                            Tools.Message(this.Page, "Xóa thành công!");
                            LoadWindowRoleByUserID(Convert.ToInt32(hdIdNguoiDung.Text));
                            FormGanQuyenReset();
                        }
                        else
                        {
                            Tools.Message(this.Page, "Xóa thất bại!");
                        }
                    }
                    catch (Exception ex)
                    {
                        Tools.Message(this.Page, "Xóa thất bại");
                    }
                    break;
            }
        }
        protected void btnUpdatePhanQuyen_click(object sender, EventArgs e)
        {
            PhanQuyenNguoiDungLogic logic = new PhanQuyenNguoiDungLogic();
            tbl_VaiTroNguoiDungDonVi objPhanQuyen = GetObjectPhanQuyenFromForm();
            long lngPhanQuyenID = TypeHelper.ToInt64(hdId.Text);
            long lngNguoiDungID = long.Parse(hdIdNguoiDung.Text);
            objPhanQuyen.pk_iVaiTroNguoiDungID = lngPhanQuyenID;
            try
            {
                if (logic.Update(objPhanQuyen))
                {
                    Tools.Message(this.Page,"Cập nhật thành công!");
                    LoadWindowRoleByUserID(lngNguoiDungID);
                    wdsPhanQuyen.ShowOnPageLoad = true;
                    btnLuuGanQuyen.Visible = false;
                    btnLuuLaiQuyen.Visible = false;
                    btnDongQuyen.Visible = true;
                    cboDonViQuanLy.ReadOnly = true;
                    cboQuyen.ReadOnly = true;
                }
                else
                {
                    Tools.Message(this.Page,"Vai trò đã tồn tại, xin kiểm tra lại dữ liệu nhập vào!");

                }
            }
            catch (Exception ex)
            {
                
                Tools.Message(this.Page,"Cập nhật thất bại!");
            }
            logic = null;
        }

        protected void btnAddNewPhanQuyen_Click(object sender, EventArgs e)
        {
            btnLuuGanQuyen.Visible = true;
            btnLuuLaiQuyen.Visible = false;
            btnDongQuyen.Visible = true;
            cboDonViQuanLy.ReadOnly = false;
            cboQuyen.ReadOnly = false;
            LoadCboDonVi();
        }
        public tbl_VaiTroNguoiDungDonVi GetObjectPhanQuyenFromForm()
        {
            tbl_VaiTroNguoiDungDonVi objRes = new tbl_VaiTroNguoiDungDonVi();
            objRes.sGhiChu = txtsGhiChu.Text.Trim();
            objRes.fk_iDonViID = long.Parse("0" + cboDonViQuanLy.Value);
            objRes.fk_iVaiTroID = long.Parse("0" + cboQuyen.Value);
            objRes.fk_iNguoiDungID = long.Parse(hdIdNguoiDung.Text);
            return objRes;
        }
        protected void btnAddPhanQuyen_click(object sender, EventArgs e)
        {
            db = new RBACDataContext(_conn);
            PhanQuyenNguoiDungLogic logic = new PhanQuyenNguoiDungLogic();
            VaiTroLogic logicVT = new VaiTroLogic();
            NguoiDungLogic logicND = new NguoiDungLogic();
            tbl_VaiTroNguoiDungDonVi objPhanQuyen = GetObjectPhanQuyenFromForm();
            long lstVaiTroID = long.Parse("0" + cboQuyen.Value);
            if (cboDonViQuanLy.SelectedIndex == null || cboDonViQuanLy.SelectedIndex < 0)
            {
                Tools.Message(this.Page,"Chưa trọn đơn vị!");
                return;
            }
            if (cboQuyen.SelectedIndex == null || cboQuyen.SelectedIndex < 0)
            {
                Tools.Message(this.Page,"Chưa trọn vai trò!");
                return;
            }
            objPhanQuyen.bKichHoat = true;
            if (logic.Insert(objPhanQuyen, lstVaiTroID))
            {
                Tools.Message(this.Page,"Gán thành công!");
                long lngNguoiDungID = long.Parse(hdIdNguoiDung.Text);
                FormGanQuyenReset();
                LoadWindowRoleByUserID(lngNguoiDungID);
            }
            
            else
            {
                Tools.Message(this.Page,"Vai trò đã tồn tại, xin kiểm tra lại dữ liệu nhập vào!");
            }
            logic = null;
        }
        protected void FormGanQuyenReset()
        {
            cboQuyen.Value = "";
            cboDonViQuanLy.Value = "";
            cboQuyen.Text = "";
            cboDonViQuanLy.Text = "";
            txtsGhiChu.Text = "";
            wdsPhanQuyen.ShowOnPageLoad = false;
        }

    }
}